<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.keyword.key" placeholder="音频名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('audio:pbaaudio:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('audio:pbaaudio:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button @click="SynchronousPrice()" type="primary">同步蜻蜓价格</el-button>
      </el-form-item>
      <el-form-item label="音频类型" style="float: right;margin-right: 60px;">
        <el-select v-model="dataForm.keyword.contentType" style="width: 120px;" @change="getDataList(1)">
          <el-option
            v-for="item in contentTypeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        width="80"
        header-align="center"
        align="center"
        prop="code"
        label="编号">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="音频名称">
      </el-table-column>
      <el-table-column
        prop="descr"
        header-align="center"
        align="center"
        label="副标题">
      </el-table-column>
      <el-table-column
        prop="coverImg"
        header-align="center"
        width="80px"
        align="center"
        label="封面图">
        <template slot-scope="scope">
          <img class="cover-img" :src="scope.row.coverImg">
        </template>
      </el-table-column>
      <el-table-column
        prop="contentTypeName"
        header-align="center"
        align="center"
        label="音频类型">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="价格">
        <template slot-scope="scope">
          <div class="price-wrapper">
            <div class="price-row">
              <span class="price-cell"></span>
              <span class="price-cell">普通</span>
              <span class="price-cell">会员</span>
            </div>
            <div class="price-row">
              <span class="price-cell">单册</span>
              <span class="price-cell">￥{{scope.row.price||0}}</span>
              <span class="price-cell">￥{{scope.row.priceVip||0}}</span>
            </div>
            <div class="price-row">
              <span class="price-cell">全套</span>
              <span class="price-cell">￥{{scope.row.pricePkg||0}}</span>
              <span class="price-cell">￥{{scope.row.pricePkgVip||0}}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="itemCount"
        header-align="center"
        align="center"
        label="子音频个数">
        <template slot-scope="scope">
          {{!isNaN(scope.row.itemCount)?scope.row.itemCount+'个':'无'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="上下架状态">
        <template slot-scope="scope">
          {{scope.row.status===1?'上架':'下架'}}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="modifyStatus(scope.row.id,scope.row.status==0?'1':'0')">
            {{scope.row.status==0?'上架':'下架'}}
          </el-button>
          <el-button type="text" size="small" @click="previewAudio(scope.row.id)">预览</el-button>
          <el-button type="text" size="small" @click="uploadSubBook(scope.row.id)">上传</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <preview-audio v-if="previewAudioVisible" ref="previewAudio" @updateAudio="updateAudio"></preview-audio>
  </div>
</template>

<script>
  import AddOrUpdate from './pbaaudio-add-or-update'
  import PreviewAudio from './pbaaudio-preview'
  export default {
    data () {
      return {
        dataForm: {
          keyword: {
            key: '',
            contentType: ''
            // ,pId: ''
          }
        },
        contentTypeOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        previewAudioVisible: false
      }
    },
    components: {
      AddOrUpdate,
      PreviewAudio
    },
    activated () {
      this.getDataList()
      this.getcontentTypeList()
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/audio/pbaaudio/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      //音频类型下拉框查询
      getcontentTypeList () {
        this.$http({
          url: this.$http.adornUrl(`/picbook/combobox/audio/listCategory`),
          method: 'post',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.contentTypeOptions = data.data
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //预览
      previewAudio (id, dataFrom) {
        this.previewAudioVisible = true
        this.$nextTick(() => {
          this.$refs.previewAudio.init(id, dataFrom)
        })
      },
      //预览弹窗跳转到修改
      updateAudio (id) {
        this.addOrUpdateHandle(id)
      },
      //预览弹窗跳转到修改
      updatePbaaudio (id) {
        this.addOrUpdateHandle(id)
      },
      //跳转音频子项去上传音频
      uploadSubBook (id) {
        this.$router.push({name: 'audio-pbaaudioitem', query: {id: id}})
      },
      //上下架
      modifyStatus (id, status) {
        this.$http({
          url: this.$http.adornUrl('/audio/pbaaudio/modifyStatus'),
          method: 'post',
          data: this.$http.adornData({
            'id': id,
            'status': status
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      SynchronousPrice () {
        this.$confirm(`确定同步蜻蜓价格?每天仅限同步一次。`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sync/syncQtPurchaseItemAndPrice'),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log('1')
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        // this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        this.$confirm(`确定[${id ? '删除' : '批量删除'}]?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/audio/pbaaudio/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style scoped lang="less">
/deep/ .el-table--medium td .cell {
    padding: 0;
  }

  .cover-img {
    padding: 0px;
    width: 50px;
    height: 70px;
  }

  .price-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    font-size: 12px;
    .price-row {
      display: flex;
      flex-direction: row;
      width: 100%;
      .price-cell {
        flex: 1;
        text-align: center;;
      }
    }
  }
</style>